package com.gxa.controller;

import com.gxa.exception.CustomerException;
import com.gxa.pojo.Diseaselog;
import com.gxa.service.DiseaselogService;
import com.gxa.util.JsonResult;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA
 * User: bear
 * Date: 2020/09/25
 * Description
 * Version: V1.0
 */
@Controller
@RequestMapping("/diseaselog")
@Transactional
public class DiseaselogController {
    @Resource
    private DiseaselogService diseaselogService;
    JsonResult jsonResult = new JsonResult();

    /**
     * 添加病情日志
     *
     * @param diseaselog 病情日志
     * @return 返回病情日志添加状态
     */
    @PostMapping("/add")
    @ResponseBody
    public Map add(@RequestBody Diseaselog diseaselog) {
        try {
            diseaselogService.add(diseaselog);
        } catch (CustomerException e) {
            String message = e.getMessage();
            e.printStackTrace();
            jsonResult.setMsg(message);
            jsonResult.setCode("500");
            return jsonResult.getValues();
        }
        jsonResult.setCode("200");
        jsonResult.setMsg("添加成功");
        return jsonResult.getValues();
    }

    /**
     * 通过患者id查询患者所有病情日志
     *
     * @param map 患者id
     * @return 所有病情日志
     */
    @PostMapping("/findByPatientIdAll")
    @ResponseBody
    public Map findByPatientIdAll(@RequestBody Map<String, Integer> map) {
        try {
            List<Diseaselog> byPatientIdAll = diseaselogService.findByPatientIdAll(map.get("patientId"));
            jsonResult.setData(byPatientIdAll);
            jsonResult.setCode("200");
            jsonResult.setMsg("所有的病情日志");
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setCode("500");
            jsonResult.setMsg("系统出小差了");
        }
        return jsonResult.getValues();
    }
}
